﻿using HBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hCommand 
{
    public abstract class LocSelectionDouble: LocSelection
    {
        protected override  CoreWhLoc select<CS>(List<CoreWhLoc> locList, long goodDiff, CS cs)
        {
            CoreWhLoc loc1 = null;
            CoreWhLoc loc2 = null;
            CoreWhLoc loc3 = null;
            CoreWhLoc loc4 = null;
            List<DoubleLoc> doubleLocList = initLoc(locList);
            foreach (DoubleLoc doubleLoc in doubleLocList)
            {
                loc1 = selectDoubleSame(loc1, doubleLoc, goodDiff);
                if (loc1 != null)
                {
                    return loc1;
                }
                loc2 = selectDeep(loc2, doubleLoc);
                if (loc2 == null)
                {
                    loc3 = selectSingle(loc3, doubleLoc);
                    if (loc3 == null)
                    {
                        loc4 = selectDoubleDef(loc4, doubleLoc, goodDiff);
                    }
                }
            }
            if (loc2 != null)
            {
                return loc2;
            }
            if (loc3 != null)
            {
                return loc3;
            }
            return loc4;
        }
        static CoreWhLoc selectDeep(CoreWhLoc select, DoubleLoc doubleLoc)
        {
            if (select != null)
            {
                return select;
            }
            CoreWhLoc deep = doubleLoc.deep;
            if (deep == null)
            {
                return select;
            }
            if (deep.countStock == 0)
            {
                return deep;
            }

            return select;
        }
        static CoreWhLoc selectDoubleSame(CoreWhLoc select, DoubleLoc doubleLoc, long goodDiff)
        {
            if (select != null)
            {
                return select;
            }

            CoreWhLoc shallow = doubleLoc.shallow;
            CoreWhLoc deep = doubleLoc.deep;
            if (deep == null)
            {
                return select;
            }
            if (deep.countStock == 0)
            {
                return select;
            }
            if (deep.workingCount() > 0)
            {
                return select;
            }
            if (deep.goodsDiff != goodDiff)
            {
                return select;
            }
            if (shallow.countStock == 0)
            {
                return shallow;
            }
            return select;
        }
        //第三种情况,单伸     
        static CoreWhLoc selectSingle(CoreWhLoc select, DoubleLoc doubleLoc)
        {

            if (select != null)
            {
                return select;
            }
            CoreWhLoc shallow = doubleLoc.shallow;
            CoreWhLoc deep = doubleLoc.deep;
            if (deep != null)
            {
                return select;
            }
            if (shallow.countStock == 0)
            {
                return shallow;
            }
            return select;
        }
        //双伸 不同
        static CoreWhLoc selectDoubleDef(CoreWhLoc select, DoubleLoc doubleLoc, long goodDiff)
        {
            CoreWhLoc shallow = doubleLoc.shallow;
            if (select != null)
            {
                return select;
            }
            CoreWhLoc deep = doubleLoc.deep;
            if (deep == null)
            {
                return select;
            }
            if (deep.countStock == 0)
            {
                return select;
            }
            if (deep.workingCount() > 0)
            {
                return select;
            }
            if (deep.goodsDiff == goodDiff)
            {
                return select;
            }
            if (shallow.countStock == 0)
            {
                return shallow;
            }
            return select;
        }


        static List<DoubleLoc> initLoc(List<CoreWhLoc> locList)
        {
            List<DoubleLoc> list = new List<DoubleLoc>();
            foreach (CoreWhLoc loc in locList)
            {
                add(loc, list);
            }
            check(list);
            return list;
        }
        static void add(CoreWhLoc loc, List<DoubleLoc> list)
        {
            bool has = false;
            foreach (DoubleLoc doublelLoc in list)
            {
                has = doublelLoc.setLoc(loc);
            }
            if (!has)
            {
                DoubleLoc doubleLoc = new DoubleLoc();
                doubleLoc.setLoc(loc);
                list.Add(doubleLoc);
            }
        }
        static void check(List<DoubleLoc> list)
        {
            List<int> removeScr = new List<int>();
            foreach (DoubleLoc doublelLoc in list)
            {
                if (doublelLoc.shallow == null)
                {
                    removeScr.Add(doublelLoc.scr);
                    LoggerCommon.consol(InfoCode.consle_loc_double_shallow_Null, doublelLoc.scr + "浅货位异常");
                }
                else if (doublelLoc.deep == null && doublelLoc.shallow.type != 1)
                {
                    removeScr.Add(doublelLoc.scr);
                    LoggerCommon.consol(InfoCode.consle_loc_double_deep_Null, doublelLoc.shallow.id + "不是单伸且深货位不存在");
                }
            }
            foreach (int value in removeScr)
            {
                list.RemoveAll(o => { return o.scr == value; });
            }
        }


    }


    class DoubleLoc
    {
        public int scr;
        public CoreWhLoc deep;
        public CoreWhLoc shallow;
        public bool setLoc(CoreWhLoc loc)
        {
            if (scr == 0)
            {
                scr = loc.scr;
            }
            else if (scr != loc.scr)
            {
                return false;
            }
            if (loc.deepIndex == 1)
            {
                if (shallow != null)
                {
                    throw new Exception(loc.id + "浅货位异常");
                }
                else
                {
                    shallow = loc;
                    return true;
                }
            }
            if (loc.deepIndex == 2)
            {
                if (deep != null)
                {
                    throw new Exception(loc.id + "深货位异常");
                }
                else
                {
                    deep = loc;
                    return true;
                }
            }
            return false;
        }

    }
}
